<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/5/18
 * Time: 上午9:38
 */

class Salesmanstat extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function articleStat()
    {
        if (!is_cli()) die('request forbidden');
        set_time_limit(300);
        try{
            $this->load->model('salesman_stat_model');
            $this->load->model('redis_model');
            $redis = $this->redis_model->getRedisInstance();
            while(true){
                //跳转链接形式海报
                $artres = $redis->lPop('slsartv2.read.stat');
                if ($artres===false) break;
                $artres = igbinary_unserialize($artres);

//                echo '1--------'.PHP_EOL;
//                var_dump($artres);echo PHP_EOL;

                //将扫描统计数据存入数据库
                $savedata = array(
                    'sls'=>$artres['sls'],
                    'model'=>$artres['model'],
                    'uid'=>$artres['uid'],
                    'time'=>$artres['timestamp'],
                    'randkey'=>$artres['randkey']
                );

//                echo '2--------'.PHP_EOL;
//                var_dump($savedata);echo PHP_EOL;

                $saveres = $this->salesman_stat_model->saveArticleStat($savedata);

//                echo '3--------'.PHP_EOL;
//                var_dump($saveres);echo PHP_EOL;

                //日志记录失败跳出
                if ($saveres===false) break;

                //队列中都是用户第一次阅读的数据，不用像海报一样排重
                $redis->multi();
                //文章全局有哪些人扫描过
                $redis->setBit('slsartv2.global.readtotalbit.'.$artres['model'],$artres['uid'],1);
                //代言人文章总收益+1
                $redis->incr('slsartv2.readtotal.'.$artres['sls']);
                //今日扫描榜单
                $today = date('Ymd',time());
                $redis->zIncrBy('slsartv2.readtotal.'.$today,1,$artres['sls']);
                $redis->zIncrBy('slssharerank.'.$today,1,$artres['sls']);
                $redis->exec();
            }
        }catch (Exception $e){
            $err['type'] = 'art';
            $err['time'] = date('Y-m-d H:i:s',time());
            $err['data'] = $e->getMessage();
            $err = json_encode($err);
            $this->redis_model->rpush('slsarterr',$err);
            $this->db->close();
            exit();
        }
    }

    public function sychArticleStat()
    {
        if (!is_cli()) die('request forbidden');
        set_time_limit(600);
        $this->load->model('salesman_stat_model');
        $this->salesman_stat_model->sychArticleStat();
    }

    public function postLinkStat()
    {
        if (!is_cli()) die('request forbidden');
        set_time_limit(300);
        try{
            $this->load->model('salesman_stat_model');
            $this->load->model('redis_model');
            $redis = $this->redis_model->getRedisInstance();
            while(true){
                //跳转链接形式海报
                $linkres = $redis->lPop('slspostv2.scan.stat');
                if ($linkres===false) break;
                $linkres = json_decode($linkres,true);

//                echo '1--------'.PHP_EOL;
//                var_dump($linkres);echo PHP_EOL;

                //将扫描统计数据存入数据库
                $savedata = array(
                    'sls'=>$linkres['sls'],
                    'postid'=>$linkres['postid'],
                    'uid'=>$linkres['uid'],
                    'time'=>$linkres['timestamp']
                );

//                echo '2--------'.PHP_EOL;
//                var_dump($savedata);echo PHP_EOL;

                $saveres = $this->salesman_stat_model->savePostStat($savedata);

//                echo '3--------'.PHP_EOL;
//                var_dump($saveres);echo PHP_EOL;

                //日志记录失败跳出
                if ($saveres===false) break;

                $isscan = $redis->getBit('slspostv2.scanbit.'.$linkres['sls'].'.'.$linkres['postid'],$linkres['uid']);
                //用户没有扫描过海报
                if ($isscan==0){
                    $redis->multi();
                    //代言人的海报哪些被扫描过
                    $redis->sAdd('slspostv2.posts.'.$linkres['sls'],$linkres['postid']);
                    //代言人海报哪些人扫描过
                    $redis->sAdd('slspostv2.scan.'.$linkres['sls'].'.'.$linkres['postid'],$linkres['uid']);
                    //海报全局有哪些人扫描过
                    $redis->setBit('slspostv2.global.postscanbit.'.$linkres['postid'],$linkres['uid'],1);
                    //代言人海报总收益+1
                    $redis->incr('slspostv2.scantotal.'.$linkres['sls']);
                    //设定用户已扫描
                    $redis->setBit('slspostv2.scanbit.'.$linkres['sls'].'.'.$linkres['postid'],$linkres['uid'],1);
                    //今日扫描榜单
                    $today = date('Ymd',time());
                    $redis->zIncrBy('slspostv2.scantotal.'.$today,1,$linkres['sls']);
                    //今日分享榜单
                    $redis->zIncrBy('slssharerank.'.$today,1,$linkres['sls']);
                    $redis->exec();
                }
            }
        }catch (Exception $e){
            $err['type'] = 'link';
            $err['time'] = date('Y-m-d H:i:s',time());
            $err['data'] = $e->getMessage();
            $err = json_encode($err);
            $this->redis_model->rpush('slsposterr',$err);
            $this->db->close();
            exit();
        }
    }

    public function postMpStat($wxtype='nldou_fuwu')
    {
        if (!is_cli()) die('request forbidden');
        set_time_limit(300);
        try{
            $this->load->model('salesman_stat_model');
            $this->load->model('redis_model');
            $redis = $this->redis_model->getRedisInstance();
            while(true){
                $mpres = $redis->lPop($wxtype.'.qrscansub.stat');
                if ($mpres===false) break;
                $mpres = json_decode($mpres,true);

//                echo '1--------'.PHP_EOL;
//                var_dump($mpres);echo PHP_EOL;

//                $mpres['openid'] = 'oHlcTt7k0U-w5QaTvRqqvDQvfOQQ';

                //获取uid

                $uidres = $this->salesman_stat_model->getUidByOpenid($mpres['openid']);

//                echo '2--------'.PHP_EOL;
//                var_dump($uidres);echo PHP_EOL;

                //用户不存在，注册新用户，获取uid
                if (empty($uidres)){
                    $uid = $this->salesman_stat_model->createUser($wxtype,$mpres['openid']);
                    //获取失败
                    if ($uid===false) break;
                }else{$uid=$uidres['uid'];}

//                echo '6--------'.PHP_EOL;
//                var_dump($uid);echo PHP_EOL;


                //将扫描统计数据存入数据库
                $savedata = array(
                    'sls'=>$mpres['info']['sls'],
                    'postid'=>$mpres['info']['postid'],
                    'uid'=>$uid,
                    'time'=>$mpres['timestamp']
                );

//                echo '7--------'.PHP_EOL;
//                var_dump($uid);echo PHP_EOL;

                $saveres = $this->salesman_stat_model->savePostStat($savedata);
                //日志记录失败跳出
                if ($saveres===false) break;

//                echo '8--------'.PHP_EOL;
//                var_dump($uid);echo PHP_EOL;

                $isscan = $redis->getBit('slspostv2.scanbit.'.$mpres['info']['sls'].'.'.$mpres['info']['postid'],$uid);
                //用户没有扫描过海报
                if ($isscan==0){
                    $redis->multi();
                    //代言人的海报哪些被扫描过
                    $redis->sAdd('slspostv2.posts.'.$mpres['info']['sls'],$mpres['info']['postid']);
                    //代言人海报哪些人扫描过
                    $redis->sAdd('slspostv2.scan.'.$mpres['info']['sls'].'.'.$mpres['info']['postid'],$uid);
                    //海报全局有哪些人扫描过
                    $redis->setBit('slspostv2.global.postscanbit.'.$mpres['info']['postid'],$uid,1);
                    //代言人海报总收益+1
                    $redis->incr('slspostv2.scantotal.'.$mpres['info']['sls']);
                    //设定用户已扫描
                    $redis->setBit('slspostv2.scanbit.'.$mpres['info']['sls'].'.'.$mpres['info']['postid'],$uid,1);
                    //今日扫描榜单
                    $today = date('Ymd',time());
                    $redis->zIncrBy('slspostv2.scantotal.'.$today,1,$mpres['info']['sls']);
                    //今日分享榜单
                    $redis->zIncrBy('slssharerank.'.$today,1,$mpres['info']['sls']);
                    $redis->exec();
                }
            }
        }catch (Exception $e){
            $err['type'] = 'mp';
            $err['time'] = date('Y-m-d H:i:s',time());
            $err['data'] = $e->getMessage();
            $err = json_encode($err);
            $this->redis_model->rpush('slsposterr',$err);
            $this->db->close();
            exit();
        }
    }

}